package com.swak.fx.support.ui;

import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.Glyph;

import com.swak.fx.support.ui.UI.Colors;
import com.swak.fx.support.ui.UI.Fonts;
import com.swak.fx.support.ui.skin.BaseLabelSkin;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Skin;
import javafx.scene.control.Tooltip;
import javafx.scene.shape.Circle;

/**
 * 图标 : 不能自定义样式表
 * 
 * @author 618lf
 */
public class UIIcon extends Glyph implements UILifecycle {

	// ================================================================================
	// Properties
	// ================================================================================
	private final String STYLE_CLASS = "ui-icon";

	public UIIcon() {
		setFontFamily("FontAwesome");
		this.init();
	}

	public UIIcon(FontAwesome.Glyph icon) {
		super("FontAwesome", icon);
		this.init();
	}

	@Override
	public void initializeParts() {
		getStyleClass().add(STYLE_CLASS);
		this.setAlignment(Pos.CENTER);
	}

	@Override
	public void setupValueChangedListeners() {
		// iconProperty().addListener((ob, o, n) -> this.setIcon(n));
	}

	/**
	 * 固定大小, 不会将文本压缩为省略号;
	 * 
	 * @return
	 */
	public UIIcon fixedWidth() {
		this.ellipsisStringProperty().bind(this.textProperty());
		return this;
	}

	public UIIcon White() {
		this.color(Colors.White);
		return this;
	}

	public UIIcon Font() {
		this.color(Colors.Font);
		return this;
	}

	public UIIcon Size(int size) {
		this.size(size);
		return this;
	}

	public UIIcon Large() {
		this.size(Fonts.Large);
		return this;
	}

	public UIIcon Small() {
		this.size(Fonts.Small);
		return this;
	}

	public UIIcon Default() {
		this.color(Colors.White);
		return this;
	}

	public UIIcon Primary() {
		this.color(Colors.Primary);
		return this;
	}

	public UIIcon Success() {
		this.color(Colors.Success);
		return this;
	}

	public UIIcon Info() {
		this.color(Colors.Info);
		return this;
	}

	public UIIcon Warning() {
		this.color(Colors.Warning);
		return this;
	}

	public UIIcon Danger() {
		this.color(Colors.Danger);
		return this;
	}

	public UIIcon Tip(String text) {
		this.setTooltip(new Tooltip(text));
		return this;
	}

	public UIIcon Round() {
		double round = this.prefWidth(-1) / 2;
		Circle circle = new Circle();
		circle.setRadius(round);
		circle.setCenterX(round);
		circle.setCenterY(round);
		this.setClip(circle);
		return this;
	}

	public UIIcon Icon(FontAwesome.Glyph icon) {
		iconProperty().set(icon);
		return this;
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private ObjectProperty<Pos> posProperty = new SimpleObjectProperty<>(Pos.TOP_RIGHT);
	private ObjectProperty<Node> decorationProperty = new SimpleObjectProperty<>(this, "decoration");
	private DoubleProperty offsetXProperty = new SimpleDoubleProperty(0);
	private DoubleProperty offsetYProperty = new SimpleDoubleProperty(0);

	public ObjectProperty<Node> decorationProperty() {
		return decorationProperty;
	}

	public ObjectProperty<Pos> posProperty() {
		return posProperty;
	}

	public DoubleProperty offsetXProperty() {
		return offsetXProperty;
	}

	public DoubleProperty offsetYProperty() {
		return offsetYProperty;
	}

	@Override
	protected Skin<?> createDefaultSkin() {
		return new UIIconSkin(this);
	}

	public class UIIconSkin extends BaseLabelSkin {
		final UIIcon control;

		public UIIconSkin(UIIcon control) {
			super(control);
			this.control = control;
			this.init();
		}

		@Override
		protected ObjectProperty<Pos> posProperty() {
			return control.posProperty();
		}

		@Override
		protected ObjectProperty<Node> decorationProperty() {
			return control.decorationProperty();
		}

		@Override
		protected DoubleProperty offsetXProperty() {
			return control.offsetXProperty();
		}

		@Override
		protected DoubleProperty offsetYProperty() {
			return control.offsetYProperty();
		}
	}
}